function trim(val)
{
	return val.replace(/(\s*$)/g, "");  
}

function tijao() {

	if (trim(document.getElementById('role_name').value) == '') {
		alert('角色名不能为空!');
		document.getElementById('role_name').focus();
		return false;
	}
	var role_name = document.getElementById('role_name').value;
	var cust_id = document.getElementById('cust_id').value;
	var flag = document.getElementById('flag').value;

 	 $.ajax({
		   type: 'POST',
		   url: 'existRole.jsp',
		   data: 'role_name='+role_name+'&cust_id='+cust_id,
		   success: function(msg){
		     if( (msg == 1) && ( flag == 0) ){
		     	alert('该角色已经被添加！');
				document.getElementById('role_name').value = '';
				document.getElementById('role_name').focus();
				return false;
		     }else {
				// 不存在同名权限，执行提交程序
					var ckvalue = '';
				var item = document.getElementsByName('one_menu_id');

				for ( var i = 0; i < item.length; i++) {
					if (item[i].checked) {
						ckvalue = item[i].value + '|' + ckvalue;
						var h = item[i].id.substring(11, 12);
						var item2 = document.getElementsByName('two_menu_id' + h);
						for ( var j = 0; j < item2.length; j++) {
							if (item2[j].checked) {
								ckvalue = item2[j].value + '|' + ckvalue;
								var h2 = item2[j].id.substring(11, 12);
								var item3 = document.getElementsByName('three_menu_id' + h2);
								for ( var k = 0; k < item3.length; k++) {
									if (item3[k].checked) {
										ckvalue = item3[k].value + '|' + ckvalue;
									}
								}
							}
						}
					}
				}
				document.getElementById('menu_id').value = ckvalue;
				if (ckvalue == '') {
					alert('请选择权限菜单');
					return false;
				}
				document.addForm.submit();
			}
		   }
		});
}

showMenuTree();

function showMenuTree(){

	var cust_id = document.getElementById('cust_id').value;
	var role_code = document.getElementById('role_code').value;

	$.ajax({
		url: 'roleajax.jsp',
	    type:'post',
		dataType:'html',
	    data: 'cust_id='+cust_id+'&role_code='+role_code,
	    success:function(msg){
	      $('#menu').html(msg);
	      $('#menu').find('dd').hide().end().find('dt').click(function() {
		         var answer = $(this).next();
		         if (answer.is(':visible')) {
		             answer.slideUp('fast');
		         } else {
		             answer.slideDown('fast');
		         }
				});
	      $('input').each(function(){
	      		if(document.getElementById('checked_menu_id').value.indexOf(this.value) != -1){
	      			this.checked = true;
	      		}
	      	})
	     }
	});
}

 //限制数字全角状态输入 允许中文输入
function   clearSBC(){ 
window.clipboardData.setData( 'text ',clipboardData.getData( 'text ').replace(/[\uFF00-\uFFFF]/g, ' ')) 
}